/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helper;
import controlacciones.ejb3.pojo.TableBeanBinding_DatosAcciones;
import java.util.*;
import jxl.*;
import jxl.write.*;
import java.io.File;

/**
 *
 * 
 */
public class Utiles_ExportarExcel {

    public Utiles_ExportarExcel() {

    }

    public void exportarReporteExcel(List lista , File file){
       escribirExcel((ArrayList) lista,file);
   }

    public void escribirExcel( ArrayList dat, File file ){       
        WritableWorkbook workbook = null;
        try{
            workbook = Workbook.createWorkbook( file );
            WritableSheet sheet = workbook.createSheet("Hoja 1", 0);
            int nfila = dat.size();
            int ncol = ( ( ArrayList ) dat.get(0) ).size();
            for( int i = 0; i < nfila; i++ ){
                for( int j = 0; j < ncol; j++ ){
                    System.out.println( "fila: " + i + " columna: " + j );
                    Label label = new Label( j, i, ( ( ArrayList )dat.get(i) ).get(j).toString() );
                    sheet.addCell(label);
                }
            }
            workbook.write();
            workbook.close();
        }catch( Exception er ){
            er.printStackTrace();
            return;
        }
    }
   
    public static boolean escribirExcelLista( List<TableBeanBinding_DatosAcciones> dat, File file ){
        boolean rpta = false;
        WritableWorkbook workbook = null;
        String celda ="";
        Label label;
        try{
            workbook = Workbook.createWorkbook( file );
            WritableSheet sheet = workbook.createSheet("Hoja 1", 0);
            int nfila = dat.size();
            int ncol = 8;//( ( TableBeanBinding_DatosAcciones ) dat.get(0) ).size();
            label = new Label( 0, 0 , "Id Accion" );
            sheet.addCell(label);
            label = new Label( 1, 0 , "NROINSCRIP" );
            sheet.addCell(label);
            label = new Label( 2, 0 , "NOMBRE" );
            sheet.addCell(label);
            label = new Label( 3, 0 , "DIRECCION" );
            sheet.addCell(label);
            label = new Label( 4, 0 , "COD TIPO_ACCION" );
            sheet.addCell(label);
            label = new Label( 5, 0 , "TIPO DE ACCION" );
            sheet.addCell(label);
            label = new Label( 6, 0 , "FEC REGISTRO" );
            sheet.addCell(label);
            label = new Label( 7, 0 , "FEC ENVIO" );
            sheet.addCell(label);

            for( int i = 0; i < nfila; i++ ){
                if(!((TableBeanBinding_DatosAcciones) dat.get(i)).getEnviada()){
                    celda = ((TableBeanBinding_DatosAcciones) dat.get(i)).getIdAccion();
                    label = new Label( 0, i + 1, celda );
                    sheet.addCell(label);

                    celda = ((TableBeanBinding_DatosAcciones) dat.get(i)).getNumInscrip();
                    label = new Label( 1, i + 1, celda );
                    sheet.addCell(label);

                    celda = ((TableBeanBinding_DatosAcciones) dat.get(i)).getNombre();
                    label = new Label( 2, i + 1, celda );
                    sheet.addCell(label);

                    celda = ((TableBeanBinding_DatosAcciones) dat.get(i)).getDireccion();
                    label = new Label( 3, i + 1, celda );
                    sheet.addCell(label);

                    celda = ((TableBeanBinding_DatosAcciones) dat.get(i)).getCod_tipo_accion();
                    label = new Label( 4, i + 1, celda );
                    sheet.addCell(label);

                    celda = ((TableBeanBinding_DatosAcciones) dat.get(i)).getDescripcionAccion();
                    label = new Label( 5, i + 1, celda );
                    sheet.addCell(label);

                    celda = ((TableBeanBinding_DatosAcciones) dat.get(i)).getFechaRegistro();
                    label = new Label( 6, i + 1, celda );
                    sheet.addCell(label);

                    celda = ((TableBeanBinding_DatosAcciones) dat.get(i)).getFechaEnvio();
                    label = new Label( 7, i + 1, celda );
                    sheet.addCell(label);
                }
            }
            workbook.write();
            workbook.close();
            rpta = true;
        }catch( Exception er ){
            er.printStackTrace();            
        }
        return rpta;
    }
}
